/******************************************************************************
 **
 **    LISA Confidential
 **
 **    Source Materials
 **
 **    (C) COPYRIGHT LISA 2008-2009
 **
 **
 **  File Name:      GetTaskListByActorIdProcDefName.java
 **  Description:    
 **  Date:           2008-12-1
 **  Author:         LisaQian
 ******************************************************************************/
package com.ti.andon.workflow.command;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.jbpm.JbpmContext;
import org.jbpm.JbpmException;
import org.jbpm.command.Command;

public class GetTaskListByActorIdProcDefNameCmd implements Command
{	
	/**
	 * Servial Version Id.
	 */
	private static final long serialVersionUID = -718207353125298875L;

	private static Log logger = LogFactory.getLog(GetTaskListByActorIdProcDefNameCmd.class);
	
	private Long actorId;
	
	private String processDefinitionName;
	
	public GetTaskListByActorIdProcDefNameCmd(Long actorId, String processDefinitionName)
	{
		super();
		this.actorId = actorId;
		this.processDefinitionName = processDefinitionName;
	}
	
	public List findOpenTasksByActorIdProcDefName(JbpmContext jbpmContext)
	{
		List result = null;
		try
		{
			Query query = jbpmContext.getSession().getNamedQuery(
					"TaskMgmtSession.findTaskInstancesByActorIdProcDefName");
			if (null != actorId)
			{
				query.setString("actorId", this.actorId.toString());
				query.setString("processDefinitionName", this.processDefinitionName);
				result = query.list();
			}
			else
			{
				logger.error("GetTaskListByActorIdProcDefNameCmd.findOpenTasksByActorIdProcDefName() : actorId can not be null!!!");
			}
		}
		catch (Exception e)
		{
			logger.error("GetTaskListByActorIdProcDefNameCmd.findOpenTasksByActorIdProcDefName() : Error occurred : "
					+ e);
			throw new JbpmException("Couldn't fin open task instances for actor '" + this.actorId
					+ "' & Process Definition Name : " + this.processDefinitionName, e);
		}
		return result;
	}

	public Object execute(JbpmContext jbpmContext) throws Exception
	{
		return findOpenTasksByActorIdProcDefName(jbpmContext);
	}
}
